<?php

namespace front\modules\app\controllers;

use common\helpers\ReturnHelper;
use front\controllers\RestBaseController;
use order\services\OrderService;
use backfeed\services\CommentService;
use goods\services\ProductService;
use users\services\UserService;
use yii\helpers\ArrayHelper;

/**
 * CommentController implements the CRUD actions for CommentModel model.
 */
class CommentController extends RestBaseController
{

    public function actionList()
    {
        $data = \yii::$app->request->post();
        $data['getByWeb'] = 1;
        $product = ProductService::ProductInfo($data['product_id']);
        $product['product'] = ArrayHelper::toArray($product);

        $product = $product['product']['result']['productInfo']['item_url_map'];
        \yii::info($product);
        $comment['list'] =[];
        if(empty($product)){
            $param['getByWeb'] = 1;
            $param['product_id'] = $data['product_id'];
            $info = CommentService::search($param);
            if($info['code'] == 0) {
                $info['result'] = ArrayHelper::toArray($info['result']);
                \yii::info($info['result']);
                if (is_array($info['result'])) {
                    foreach ($info['result'] as $com) {
                        $arr['created_at'] = date('Y-m-d H:i:s', $com['created_at']);
                        $arr['content'] = $com['content'];
                        $arr['product'] = ['product_id' => $data['product_id']];
                        $user = UserService::getUserInfo($com['user_id']);
                        $user = ArrayHelper::toArray($user['result']);
                        $mobile = $user['mobile'];
                        $replace = substr($mobile,3,4);
                        $mobile = str_replace($replace,'****',$mobile);
                        $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                        array_push($comment['list'], $arr);
                    }
                }
            }
        }else {
            foreach ($product as $key => $item) {
                if ($item['product_id'] != $data['product_id']) {
                    continue;
                }
                $item['getByWeb'] = 1;
                $info = CommentService::search($item);
                if ($info['code'] == 0) {
                    $info['result'] = ArrayHelper::toArray($info['result']);
                    \yii::info($info['result']);
                    if (is_array($info['result'])) {
                        foreach ($info['result'] as $com) {
                            $arr['created_at'] = date('Y-m-d H:i:s', $com['created_at']);
                            $arr['content'] = $com['content'];
                            $arr['product'] = $product[$key];
                            $user = UserService::getUserInfo($com['user_id']);
                            $user = ArrayHelper::toArray($user['result']);
                            $mobile = $user['mobile'];
                            $replace = substr($mobile,3,4);
                            $mobile = str_replace($replace,'****',$mobile);
                            $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                            array_push($comment['list'], $arr);
                        }
                    }
                }
            }
        }
        $created_at = array();
        foreach ($comment['list'] as $item) {
            $created_at[] = $item['created_at'];
        }
        array_multisort($created_at, SORT_DESC, $comment['list']);
        $comment['count'] = count($comment['list']);
        return ReturnHelper::success('success', $comment);
    }


    /**
     * 插入评论
     */

    public function actionAdd()
    {
        $data = \yii::$app->request->post();
        $data['product_id'] = intval($data['product_id']);
        $orderId = intval($data['order_id']);
        $data['created_at'] = time();
        $data['order_id'] = $orderId;
        $data['content'] = strip_tags($data['content']);
        $result = CommentService::add($data);
        if ($result['code'] == 0) {
            $status = OrderService::changeStatus([0 => ['order_id' => $orderId,
                'status' => 6,
            ]]);
            if ($status['code'] != 0) {
                return ReturnHelper::error($status['message']);
            }
            return ReturnHelper::success("success");
        }
        return ReturnHelper::error($result['message']);
    }

    /**
     * 获取评论页面信息
     * @return array
     */
    public function actionDetail()
    {
        $data['product_id'] = intval(\yii::$app->request->post('product_id'));
        $product = ProductService::ProductInfo($data['product_id']);
        if($product['code'] == 0) {
            $product = ArrayHelper::toArray($product['result']);
            $info['product']['attributes'] = $product['productInfo']['item_url_map'];
            foreach ($info['product']['attributes'] as $item) {
                if ($data['product_id'] == $item['product_id']) {
                    $info['product']['attributes'] = $item;
                    break;
                }
            }
            $info['product']['thumb'] = $product['productInfo']['thumb'][0];
            $info['product']['name'] = $product['productInfo']['name'];
            $info['product']['price'] = static::price_format($product['productInfo']['price']);
            $info['product_id'] = $data['product_id'];
            return ReturnHelper::success('success', $info);
        }
        return ReturnHelper::error('该商品不存在');
    }

}
